package com.woniu.user.controller;

import com.woniu.common.entity.*;
import com.woniu.user.service.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
public class ApplyController {
    @Autowired
    ApplyService applyService;
    //犬只审核---申请界面查询
    @RequestMapping("/selectApply")
    public List<ApplyForTable> selectApply(){
        List<ApplyForTable> list=applyService.selectApplies();
        return list;
    }
    //犬只审核---个人（犬只等）详情请界面查询
    @RequestMapping("/selectDate")
    public ApplyForTable selectDate(String icd){
        System.out.println(icd);
        ApplyForTable list=applyService.selectDate(icd);
        return list;
    }
    //犬只审核---审核通过
    @RequestMapping("/applySuccess1")
    public void applySuccess1(String icd,String reviewer){
        System.out.println(icd);
        System.out.println(reviewer);
        int i=1;
        int j=1;
        applyService.selectApply(icd,i,j,reviewer);
        //将个人信息和犬只信息登记入库
        ApplyForTable list=applyService.selectDate(icd);
        Dog dog=new Dog();
        dog.setDname(list.getDog_name());
        dog.setSex(list.getDog_sex());
        dog.setType(list.getDog_type());
        dog.setWeight(list.getDog_weight());
        dog.setHeight(list.getDog_heigh());
        dog.setLongth(list.getDog_long());
        dog.setDog_positive_image(list.getDog_positive_image());
        dog.setDog_side_image(list.getDog_side_image());
        dog.setRegist_date(list.getApply_date());
        dog.setU_id(list.getU_id());

        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        System.out.println(uuid);
        dog.setNum_dog(uuid);

        System.out.println(dog);
        applyService.insertDog(dog);
        //通知消息

        Message message=new Message();
        message.setTitle("养犬登记审核通知");
        String a="你的养犬申请已通过，请到指定地点登记犬证---------"+"犬名："+dog.getDname()+",犬牌编号："+dog.getNum_dog();
        message.setContent(a);
        int b=1;
        message.setType(b);
        message.setU_id(dog.getU_id());
        int c=0;
        message.setStatus(c);
        message.setMessage_time(new Date());
        applyService.insertMessage(message);




    }
    //犬只审核---个人审核wei通过
    @RequestMapping("/applySuccess2")
    public void applySuccess2(String icd,String reason,String reviewer){
        System.out.println(icd);
        System.out.println(reason);

        int i=2;

        applyService.selectApply2(icd,i,reason,reviewer);

        //通知消息

        ApplyForTable list=applyService.selectDate(icd);
        Message message=new Message();
        message.setTitle("养犬登记审核通知");
        String a="你的养犬申请未通过---------"+"未通过原因："+reason;
        message.setContent(a);
        int b=1;
        message.setType(b);
        message.setU_id(list.getU_id());
        int c=0;
        message.setStatus(c);
        message.setMessage_time(new Date());
        applyService.insertMessage(message);
    }
    //犬只审核---犬只审核wei通过
    @RequestMapping("/applySuccess3")
    public void applySuccess3(String icd,String reason,String reviewer){
        System.out.println(icd);
        System.out.println(reason);
        int i=1;
        int j=2;
        applyService.selectApply3(icd,i,j,reason,reviewer);

        //通知消息

        ApplyForTable list=applyService.selectDate(icd);
        Message message=new Message();
        String title="养犬登记审核通知";
        //message.setTitle(title);
        String content="你的养犬申请未通过---------"+"未通过原因："+reason;
        //message.setContent(content);
        int type=1;
       // message.setType(type);
        int u_id=list.getU_id();
        //message.setU_id(u_id);
        //int setStatus=0;
        //message.setStatus(setStatus);
        //Date message_time=new Date();
        //message.setMessage_time(new Date());
        //applyService.insertMessage(message);
        insertMessage(title,content,type,u_id);
    }
    //消息通知接口
    @RequestMapping("/insertMessage")
    public ResponseResult<String> insertMessage(String title, String content, int type, int u_id){
        Message message=new Message();
        message.setTitle(title);
        message.setContent(content);
        message.setType(type);
        message.setU_id(u_id);

        int setStatus=0;
        message.setStatus(setStatus);
        message.setMessage_time(new Date());
        applyService.insertMessage(message);

        return new ResponseResult<String>(200,null,"成功",null,null);
    }
    //犬牌登记
    @RequestMapping("/petBrandLogin")
    public void petBrandLogin(String etcnum,String url,String name,int uid){
        System.out.println(etcnum);
        System.out.println(url);
        System.out.println(name);
        System.out.println(uid);
        applyService.updateDognum(etcnum,url,name,uid);
    }
    //审核通过的犬只及主人信息查询
    @RequestMapping("/selectDogs")
    public List<Dog> selectDogs(){
        List<Dog> dog= applyService.selectDog();
        return dog;
    }
    //个人消息查询
    @RequestMapping("/selectMesage")
    public List<Message> selectMesage(String account){
        int uid=applyService.selectUser(account).getId();
        List<Message> list=applyService.selectMessage(uid);
        return list;
    }
    //修改个人消息status
    @RequestMapping("/updateStatus")
    public void updateStatus(int id){
        int status=1;
        applyService.updateStatus(status, id);

    }
    //投诉消息查询
    @RequestMapping("/selectComplain")
    public ResponseResult<List<Complain>> selectComplain(){

        List<Complain> list=applyService.selectComplainMessage();
        return new ResponseResult<List<Complain>>(200,null,null,list,null);
    }
    //投诉消息回复
    @RequestMapping("/responseMessage")
    public ResponseResult<String> responseMessage(int id, String token){
        int status=2;
        applyService.updateComplainMessage(id, status);
        return new ResponseResult<String>(200,null,null,"成功",null);
    }






}
